home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / lib / c / input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-04  |  4.5 KB  |  236 lines

  1. /* 
  2. ** <<ACE>> linked library module: DOS and Intuition window INPUT code. 
  3. ** Copyright (C) 1998 David Benn
  4. ** 
  5. ** This program is free software; you can redistribute it and/or
  6. ** modify it under the terms of the GNU General Public License
  7. ** as published by the Free Software Foundation; either version 2
  8. ** of the License, or (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program; if not, write to the Free Software
  17. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18. **
  19. ** Author: David J Benn
  20. **   Date: 10th,11th October 1993,
  21. **       5th December 1993
  22. */
  23.  
  24. #include <exec/types.h>
  25. #include <intuition/intuition.h>
  26.  
  27. #define BOOL     short
  28. #define TRUE    1
  29. #define FALSE    0
  30.  
  31. /* external variables */
  32. extern     BYTE     IntuiMode;
  33. extern    SHORT    fgdpen;
  34. extern     ULONG     stdin,stdout;
  35. extern     SHORT    Wdw_id;
  36. extern    struct    Screen *Scrn;
  37. extern     struct     Window *Wdw;
  38. extern     struct    RastPort *RPort;
  39.  
  40. /* globals */
  41. static    char space = ' ';
  42.  
  43. /* external functions */
  44. extern    void    scroll_screen();
  45.  
  46. /* functions */
  47. void cursor()
  48. {
  49. char     csr = '|';
  50. ULONG    i,oldfg,maxcolr,x,y;
  51. /* Display cursor in Intuition window */
  52.  
  53.     /* change foreground colour */
  54.     oldfg = fgdpen;
  55.     /* maximum colour-id = 2^depth-1 */
  56.     maxcolr = 1;
  57.     for (i=1;i<=Scrn->BitMap.Depth;i++) maxcolr <<= 1;
  58.     --maxcolr;
  59.     SetAPen(RPort,maxcolr);
  60.             
  61.     /* show cursor */
  62.     Text(RPort,&csr,1L);
  63.     x = RPort->cp_x - RPort->Font->tf_XSize;
  64.     y = RPort->cp_y;
  65.     Move(RPort,x,y);
  66.  
  67.     /* restore foreground colour */
  68.     SetAPen(RPort,oldfg);
  69. }
  70.  
  71. BOOL eoln(ch)
  72. {
  73. /* End of line? */
  74.  
  75.     if (ch == '\n' || ch == '\r')
  76.        return(TRUE);
  77.     else
  78.        return(FALSE);    
  79. }
  80.  
  81. void process_char(ch,ccol,buf)
  82. char ch;
  83. long *ccol;
  84. char *buf;
  85. {
  86. ULONG x,y;
  87. /* Display a character on a DOS or Intuition window
  88.    or handle a non-printable character. 
  89. */
  90.  
  91.     /* display current character */
  92.     if (IntuiMode == 0)
  93.     {
  94.         /* DOS window */
  95.         if (ch == '\r') ch = '\n';   /* RAW: eoln = CR -> LF */
  96.         if (ch != '\b')
  97.                Write(stdout,&ch,1L);
  98.         else
  99.         if (*ccol > 0) 
  100.         {
  101.             /* erase character? */
  102.             putchar('\b');
  103.             putchar(' ');
  104.             putchar('\b');    
  105.         }        
  106.     }
  107.     else
  108.     {
  109.         /* Intuition window */
  110.         if (ch != '\b') 
  111.         {
  112.             if (!eoln(ch))
  113.             {
  114.                    Text(RPort,&ch,1L);
  115.                 cursor();
  116.             }
  117.         }
  118.         else
  119.         if (*ccol > 0)
  120.         {
  121.             /* erase character */
  122.             x = RPort->cp_x - RPort->Font->tf_XSize;
  123.             y = RPort->cp_y;
  124.             Move(RPort,x,y);
  125.             Text(RPort,&space,1L);
  126.             Text(RPort,&space,1L);
  127.             x = RPort->cp_x - ((RPort->Font->tf_XSize) << 1);
  128.             y = RPort->cp_y;
  129.             Move(RPort,x,y);
  130.             cursor();
  131.         }
  132.     } 
  133.  
  134.     /* delete or store a character */
  135.     if (ch == '\b') 
  136.     {
  137.            /* delete rightmost character */
  138.            if (*ccol > 0) *ccol -= 1;
  139.     }
  140.     else
  141.     {
  142.         /* store character & advance to next */
  143.         if (!eoln(ch))
  144.         {
  145.             buf[*ccol] = ch;
  146.             *ccol += 1;
  147.         }
  148.     }    
  149. }
  150.  
  151. char input_char()
  152. {
  153. char       ch;
  154. struct     IntuiMessage *msg;
  155. BOOL    no_key_press=TRUE;
  156. /* Get a character from a DOS or Intuition window. */
  157.  
  158.     if (IntuiMode == 0)
  159.     {
  160.         /* DOS window */
  161.         while (!WaitForChar(stdin,1L));
  162.         Read(stdin,&ch,1L);
  163.     }
  164.     else
  165.     {
  166.         /* Intuition window */
  167.         do
  168.         {
  169.             WaitPort(Wdw->UserPort);
  170.             msg = (struct IntuiMessage *)GetMsg(Wdw->UserPort);
  171.             if (msg && msg->Class & VANILLAKEY)
  172.             {
  173.                 ch = (msg->Code & 0xff);
  174.                 no_key_press = FALSE;
  175.                 ReplyMsg(msg);
  176.             }
  177.             else
  178.                 if (msg) ReplyMsg(msg);
  179.         }
  180.         while (no_key_press);
  181.     } 
  182.  
  183.     return(ch);
  184. }
  185.  
  186. void Ustringinput(buf)
  187. char *buf;
  188. {
  189. char     ch;
  190. ULONG    x,y;
  191. long    bytes,ccol=0;
  192. static    char csr_on[]     = { 0x9b,0x20,0x70 };
  193. static    char csr_off[]     = { 0x9b,0x30,0x20,0x70 };
  194. /* Input a string from a DOS or Intuition window. */
  195.  
  196.     /* turn cursor off */
  197.     if (IntuiMode == 0) 
  198.        Write(stdout,csr_on,3L); 
  199.     else
  200.        cursor();
  201.        
  202.     /* CON: ? */
  203.     if (Wdw_id == 0 && IntuiMode == 0) 
  204.     { 
  205.         bytes = Read(stdin,buf,1024L); 
  206.         buf[bytes-1] = '\0'; 
  207.     }
  208.     else
  209.     {
  210.         /* RAW: window or Intuition window */
  211.         do
  212.         {
  213.             ch = input_char();
  214.             process_char(ch,&ccol,buf);
  215.         }
  216.         while (!eoln(ch));
  217.  
  218.         buf[ccol] = '\0';
  219.     }
  220.  
  221.     /* turn cursor off */
  222.     if (IntuiMode == 0) 
  223.        Write(stdout,csr_off,4L);
  224.        else
  225.     {
  226.        /* overwrite cursor */     
  227.          Text(RPort,&space,1L);
  228.  
  229.        /* move to next line in Intuition window */
  230.        x = 1;
  231.        y = RPort->cp_y + RPort->Font->tf_YSize;
  232.        Move(RPort,x,y);
  233.        scroll_screen();
  234.     }
  235. }
  236.